home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / windows / doc / java / threads / classes / raceapplet.java < prev    next >
Encoding:
Java Source  |  1996-02-26  |  2.7 KB  |  91 lines

  1. /*
  2.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17.  
  18. import browser.Applet;
  19. import awt.*;
  20.  
  21. class RaceApplet extends Applet implements Runnable {
  22.  
  23.     final static int NUMRUNNERS = 2;
  24.     final static int SPACING = 20;
  25.  
  26.     Runner runners[] = new Runner[NUMRUNNERS];
  27.  
  28.     Thread updateThread;
  29.  
  30.     public void init() {
  31.     String raceType = getAttribute("type");
  32.     for (int i = 0; i < NUMRUNNERS; i++) {
  33.         runners[i] = new Runner();
  34.         if (raceType.compareTo("unfair") == 0)
  35.             runners[i].setPriority(i+1);
  36.         else
  37.             runners[i].setPriority(2);
  38.         }
  39.         if (updateThread == null) {
  40.             updateThread = new Thread(this, "Thread Race");
  41.             updateThread.setPriority(NUMRUNNERS+1);
  42.         }
  43.     }
  44.  
  45.     public void mouseDown(int x, int y) {
  46.     if (!updateThread.isAlive())
  47.             updateThread.start();
  48.     for (int i = 0; i < NUMRUNNERS; i++) {
  49.         if (!runners[i].isAlive())
  50.             runners[i].start();
  51.     }
  52.     }
  53.  
  54.     public void paint(Graphics g) {
  55.         g.setForeground(Color.lightGray);
  56.         g.fillRect(0, 0, width, height);
  57.         g.setForeground(Color.black);
  58.         for (int i = 0; i < NUMRUNNERS; i++) {
  59.         int pri = runners[i].getPriority();
  60.         g.drawString(new Integer(pri).toString(), 0, (i+1)*SPACING);
  61.     }
  62.         update(g);
  63.     }
  64.  
  65.     public void update(Graphics g) {
  66.         for (int i = 0; i < NUMRUNNERS; i++) {
  67.         g.drawLine(SPACING, (i+1)*SPACING, SPACING + (runners[i].tick)/1000, (i+1)*SPACING);
  68.     }
  69.     }
  70.  
  71.     public void run() {
  72.         while (updateThread != null) {
  73.             repaint();
  74.             updateThread.sleep(10);
  75.         }
  76.     }    
  77.  
  78.     public void stop() {
  79.     for (int i = 0; i < NUMRUNNERS; i++) {
  80.         if (runners[i].isAlive()) {
  81.             runners[i].stop();
  82.             runners[i] = null;
  83.         }
  84.         }
  85.     if (updateThread.isAlive()) {
  86.             updateThread.stop();
  87.             updateThread = null;
  88.     }
  89.     }
  90. }
  91.